public class Solution665 {
    public boolean checkPossibility(int[] nums) {
        int t=-1;
        int v=-1;
        for (int i=0;i<nums.length-1;i++){
            if (nums[i]>nums[i+1]){
                t=i;
                v=nums[i];
                nums[i]=nums[i+1];
                break;
            }
        }
        if (t==-1){
            return true;
        }
        int flag=-1;
        for (int i=0;i<nums.length-1;i++){
            if (nums[i]>nums[i+1]){
                flag=1;
                break;
            }
        }
        if (flag==-1){
            return true;
        }
        nums[t]=nums[t+1]=v;
        flag=-1;
        for (int i=0;i<nums.length-1;i++){
            if (nums[i]>nums[i+1]){
                flag=1;
                break;
            }
        }
        if (flag==-1){
            return true;
        }else{
            return false;
        }
    }

    public static void main(String[] args) {
        System.out.println(new Solution665().checkPossibility(new int[]{5,7,1,8}));
    }
}
